package br.com.mackinvest.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.TreeMap;

import br.com.mackinvest.constants.Constants;
import br.com.mackinvest.model.Cliente;

public class ClienteTextDAO implements GenericDAO<Cliente> {
	
	private File arquivoCliente;
 
	public ClienteTextDAO() {
		super();
	}
	
	public ClienteTextDAO(String path) {
		super();
		arquivoCliente = new File(path);
	}


	public void salva(Cliente model) 
	{
		try
		{
			if ( model.getId_contaCorrente() < 1 )
			{
				model.setId_contaCorrente( model.getContaCorrente().getId() );			
			}
			
			FileOutputStream fout = new FileOutputStream(arquivoCliente, true);
			BufferedOutputStream bout = new BufferedOutputStream(fout);
			ObjectOutputStream oout = new ObjectOutputStream(bout);
			
			
			oout.writeObject( model );
			
			oout.flush();
			
			oout.close();
			bout.close();
			fout.close();
		}
		catch ( IOException ex )
		{
			System.out.println(Constants.ERRO_CONEXAO_ARQUIVO_TEXTO);
		}
		
	}
	 
	public void deleta(Cliente model) 
	{
		try
		{
			//lendo pra encontrar o cliente
			FileInputStream fin = new FileInputStream(arquivoCliente);
			BufferedInputStream bin = new BufferedInputStream(fin);
			ObjectInputStream oin = new ObjectInputStream(bin);
			
			ArrayList<Cliente> listaCliente = new ArrayList<Cliente>();
			
			Object temp = null;
			while ( ( temp = oin.readObject() ) != null )
			{
				listaCliente.add( (Cliente)temp );
			}

			oin.close();
			bin.close();
			fin.close();
			
			if (listaCliente.contains( model ))
				listaCliente.remove( model );
			
			
			//gravando todo o arquivo de novo!
			FileOutputStream fout = new FileOutputStream(arquivoCliente, false);
			BufferedOutputStream bout = new BufferedOutputStream(fout);
			ObjectOutputStream oout = new ObjectOutputStream(bout);

			for ( Cliente cliente : listaCliente )
			{
				oout.writeObject( cliente );
			}
			
			oout.flush();
			
			oout.close();
			bout.close();
			fout.close();
		}
		catch ( IOException ex )
		{
			System.out.println(Constants.ERRO_CONEXAO_ARQUIVO_TEXTO);
		}
		catch ( ClassNotFoundException ex )
		{
			System.out.println(Constants.ERRO_CONTEUDO_ARQUIVO_TEXTO);
		}

			 
	}
	 
	public void atualiza(Cliente model) 
	{
		try
		{
			//lendo pra encontrar o cliente
			FileInputStream fin = new FileInputStream(arquivoCliente);
			BufferedInputStream bin = new BufferedInputStream(fin);
			ObjectInputStream oin = new ObjectInputStream(bin);
			
			ArrayList<Cliente> listaCliente = new ArrayList<Cliente>();
			
			Object temp = null;
			while ( ( temp = oin.readObject() ) != null )
			{
				listaCliente.add( (Cliente)temp );
			}

			
			oin.close();
			bin.close();		
			fin.close();
			
			//se já existir um na lista com o mesmo ID eu substituo...
			int indexModel = listaCliente.indexOf( model );
			if ( indexModel > -1 )
				listaCliente.set( indexModel , model);
			
			//gravando todo o arquivo de novo!
			FileOutputStream fout = new FileOutputStream(arquivoCliente, false);
			BufferedOutputStream bout = new BufferedOutputStream(fout);
			ObjectOutputStream oout = new ObjectOutputStream(bout);

			for ( Cliente cliente : listaCliente )
			{
				oout.writeObject( cliente );
			}
			
			oout.flush();
			
			oout.close();
			bout.close();
			fout.close();
		}
		catch ( IOException ex )
		{
			System.out.println(Constants.ERRO_CONEXAO_ARQUIVO_TEXTO);
		}
		catch ( ClassNotFoundException ex )
		{
			System.out.println(Constants.ERRO_CONTEUDO_ARQUIVO_TEXTO);
		}
	 
	}
	 
 
	public TreeMap<Integer, Cliente> retornaTodos() 
	{
		TreeMap<Integer, Cliente> clientes = new TreeMap<Integer, Cliente>();
		try
		{
			//lendo pra encontrar o cliente
			FileInputStream fin = new FileInputStream(arquivoCliente);
			BufferedInputStream bin = new BufferedInputStream(fin);
			ObjectInputStream oin = new ObjectInputStream(bin);
			
			Object temp = null;
			Cliente clienteTemp = null;
			while ( ( temp = oin.readObject() ) != null )
			{
				clienteTemp = (Cliente)temp; 
				clientes.put( clienteTemp.getId(), clienteTemp );
			}

			oin.close();
			bin.close();
			fin.close();
		}
		catch ( IOException ex )
		{
			System.out.println(Constants.ERRO_CONEXAO_ARQUIVO_TEXTO);
		}
		catch ( ClassNotFoundException ex )
		{
			System.out.println(Constants.ERRO_CONTEUDO_ARQUIVO_TEXTO);
		}
		return clientes;
	}
	 
	
	public Cliente procuraPorExemplo(Cliente model) {
		return null;
	}
	 
}

 
